package sim

import (
	"context"

	"github.com/gogf/gf/frame/g"
	"github.com/sagoo-cloud/sagooiot/internal/service"
	"github.com/sagoo-cloud/sagooiot/internal/task/sim/common"
	"github.com/sagoo-cloud/sagooiot/internal/task/sim/ctwing"
	"github.com/sagoo-cloud/sagooiot/internal/task/sim/factory"
)

// SyncSimList 定时获取sim卡
func SyncSimList(ctx context.Context, workerName string) {
	g.Log().Info(ctx, workerName, " start SyncSimList")
	out, err := service.SimSimFactory().GetSimFactoryByType(ctx, 1)
	if err != nil {
		g.Log().Error(ctx, workerName, "startCtwing get factory error ", err)
		return
	}
	ins := factory.MakeWoker(workerName)
	common.Pool.Add(ctx, func(ctx context.Context) {
		ins.SimList(ctx, out.AppKey, out.AppSecret, "", ctwing.Ctwing_Sim_List, 1)
	})
}

// SyncRealFlow 定时获取实时流量
func SyncRealFlow(ctx context.Context, workerName string) {
	g.Log().Info(ctx, workerName, " start SyncRealFlow")
	out, err := service.SimSimFactory().GetSimFactoryByType(ctx, 1)
	if err != nil {
		g.Log().Error(ctx, workerName, "startCtwing get factory error ", err)
		return
	}
	ins := factory.MakeWoker(workerName)
	common.Pool.Add(ctx, func(ctx context.Context) {
		ins.QueryRealTraffic(ctx, out.AppKey, out.AppSecret, ctwing.Ctwing_Batch_Flow_ByMonth, 1)
	})

}

// SyncYesterdayFlow 定时获取昨日流量,形成历史记录
func SyncYesterdayFlow(ctx context.Context, workerName string) {
	g.Log().Info(ctx, workerName, " start SyncYesterdayFlow")
	out, err := service.SimSimFactory().GetSimFactoryByType(ctx, 1)
	if err != nil {
		g.Log().Error(ctx, workerName, "startCtwing get factory error ", err)
		return
	}
	ins := factory.MakeWoker(workerName)
	common.Pool.Add(ctx, func(ctx context.Context) {
		ins.QueryHistoryTraffic(ctx, out.AppKey, out.AppSecret, ctwing.Ctwing_Batch_YesterdayFlow, 1)
	})
}
